<?php
/**
 * @Author: Zhao Yang[873777808@qq.com]
 * @Date:   2015-02-23 23:29:29
 * @Last Modified time: 2015-11-23 23:47:35
 */
// 后台登陆控制器
namespace Admin\Controller;
use Think\Controller;

class LoginController extends Controller{
	public function index()
	{
		// 判断 是否登陆
		if(session('user_auth')){
			$this->redirect('Admin/Index/index');
		}else{
			$this->display();
		}
	}

	// 后台登陆提交
	public function ajaxLogin()
	{
		if(IS_POST){
			// 验证码是否正确
			if(!checkCode(I('post.code'))) $this->error('验证码错误');
			$name = I('post.name');
			$pwd = I('post.pwd');
			$db = M('Admin');
			$data = $db->where(array('name'=>$name))->find();
			if(md5($pwd.$data['code'])!==$data['pwd']){
				$this->error('用户名或密码错误');
			}else{
				// 更新登陆信息
				unset($data['pwd']);
				if($data['islock']==1){
					$this->error('你的账号已经被限制登陆');
				}
				
				$info = array(
	            'uid'       => $data['uid'],
	            'logintime' => NOW_TIME,
	            'loginip'   => get_client_ip(),
        		);
				$db->save($info);
				// 设置session
				$auth = array(
				    'uid'  => $data['uid'],
				    'name' => $data['name']
				);
				session('user_auth', $auth);
        session('user_auth_sign', data_auth_sign($auth));
				$this->success('登陆成功');
			}
		}else{
			$this->error('页面不存在');
		}
	}

	// 验证码显示
	public function code()
	{
		$Verify = new \Think\Verify(C('CODE_CONFIG'));
		$Verify->entry();
	}

	/**
	 * 退出操作
	 * @return [type] [description]
	 */
	public function logout()
	{
		session('user_auth', null);
    session('user_auth_sign', null);
    $this->success('退出成功',U('index'));
	}
}